package com.vilen.util;

import com.google.zxing.qrcode.decoder.ErrorCorrectionLevel;
import net.glxn.qrgen.core.image.ImageType;
import net.glxn.qrgen.javase.QRCode;

import java.io.*;

/**
 *  生成微信二维码扫一扫方法，并实现登录系统
 * Created by vilen on 2016/1/11.
 */
public class QrcodeUtil {
    /**
     * 生成二维码图片
     *
     * @param content 生成二维码的内容
     * @param imgPath 需要保存二维码图片的路径
     */
    public static void QrcodeImg1(String content,String imgPath) {
        //l(7%) m(15%) q(25%) h(30%)
        File file = QRCode.from(content)
                .withCharset("UTF-8")
                .withErrorCorrection(ErrorCorrectionLevel.L)
                .withSize(140,140)
                .file();

    }
    public static void QrcodeImg2(String content,String imgPath) {
        ByteArrayOutputStream out = QRCode.from(content)
                .withCharset("UTF-8")
                .withErrorCorrection(ErrorCorrectionLevel.L)
                .withSize(140,140)
                .to(ImageType.PNG).stream();
        try
        {
            FileOutputStream fout = new FileOutputStream(new File(imgPath));
            fout.write(out.toByteArray());
            fout.flush();
            fout.close();
        }
        catch (FileNotFoundException e)
        {
            e.printStackTrace();
        }
        catch (IOException e)
        {
            e.printStackTrace();
        }
    }

    public static void main(String[] args) {
        String content = "my name is Vilen!";
        QrcodeImg2(content,"D:/QR_Code.jpg");

    }
}
